package com.kurumi.medical.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kurumi.medical.pojo.Result;
import com.kurumi.medical.pojo.User;
import com.kurumi.medical.util.HttpClientUtil;


public class ShiroRealmNewImpl extends AuthorizingRealm {


	
	@Value("${login.check.url}")
	private String loginCheckUrl;
	

	
	@Value("${findAuthorityList.url}")
	private String findAuthorityListUrl;
	
	/**
	 * 为当前登录的用户授予角色和权限
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		Map<String, String> params = new HashMap<String, String>();
		
		Subject subject=SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User) session.getAttribute("user");
		
		params.put("userId", user.getId());
		params.put("medicalMainstayId", user.getMedicalMainstayId());
		
		String authorityStr = HttpClientUtil.doPost(findAuthorityListUrl, params);
		Result result = JSONObject.parseObject(authorityStr, Result.class);
		Set<String> authoritySet = new HashSet<String>();
		if(result.isSuccess()) {
		   JSONArray authJsonArray =  (JSONArray) result.getData();
		   for(int i = 0; i < authJsonArray.size(); i++) {
			   authoritySet.add((String)authJsonArray.get(i));
		   }
		   
		}
		
		//设置权限
		SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
		authorizationInfo.setStringPermissions(authoritySet);
		
		
		return authorizationInfo;
		
	}

	
	/**
	 * 用户登录验证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String loginName = (String)token.getPrincipal();
		Subject subject=SecurityUtils.getSubject();
		Session session = subject.getSession();
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("username", loginName);
		params.put("contextPath", (String)session.getAttribute("contextPath"));
		//远程调用sso登录系统
	    String userJson = HttpClientUtil.doPost(loginCheckUrl, params);
	    
	    JSONObject jsonObject = JSONObject.parseObject(userJson);
	    if((Boolean)jsonObject.get("isSuccess")) {
	    	String userName = (String) jsonObject.get("usreName");
	    	String password = (String) jsonObject.get("password");
	    	AuthenticationInfo authcInfo=new SimpleAuthenticationInfo(userName, password, "xx");
			return authcInfo;
			
	    } else {
	    	return null;
	    }
		
	}
	
	
	
	
}

